package com.example.ljy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.example.ljy.activity.BleActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    Button LianJieBtn;
    Button LxBtn;//连续
    Button setBtn;
    String city;
    TextView textView;
    TextView WeathText;
    int m ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Intent intent = this.getIntent();
         m = intent.getIntExtra(getString(R.string.times),0);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_NETWORK_STATE}, 1000);
        }
        city="大连";
        searchCity("大连");
        //m = getdates("dates");
        //textView.setText("你已经连续打卡"+m+"次");

    }
    private void searchCity(String district) {
        //使用Get异步请求
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                //拼接访问地址
                .url("https://v0.yiketianqi.com/api?unescape=1&version=v61&appid=25862794&appsecret=ErQ8efYH&city="+district)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("a","获取数据失败了");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"获取数据失败了",Toast.LENGTH_SHORT).show();
                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){//回调的方法执行在子线程。
                    Message message = handler.obtainMessage();
                    String string = response.body().string();
                    message.obj= string;
                    handler.sendMessage(message);
                    Log.e("a","获取数据成功了");
                    Log.d("a","response.code()=="+response.code());
                   // Log.d("a","response.body().string()=="+response.body().string());
                }
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //Response response = (Response) msg.obj;
            try {
                JSONObject toJsonObj= new JSONObject(msg.obj.toString());
                Log.e("fff", "handleMessage: "+toJsonObj.getString("cityid") );
                String we=city+" : "+toJsonObj.getString("tem2")+"~"+toJsonObj.getString("tem1")+"°C  "+toJsonObj.getString("wea");
                WeathText.setText(we);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };
    private void initView() {
        LianJieBtn = findViewById(R.id.dakaBtn);
        setBtn = findViewById(R.id.settingBtn);
        LxBtn = findViewById(R.id.lianXuBtn);
        textView = findViewById(R.id.text);
        WeathText = findViewById(R.id.text_weather);
        LianJieBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一种
//                Intent intent =  new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
//                startActivity(intent);
                Intent intent =  new Intent(MainActivity.this, BleActivity.class);
                startActivity(intent);
                //第二种
                //startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));//直接跳转到设置界面
                //LianJieBtn.setEnabled(false);
                //int a = m+1;
                //textView.setText("你已经连续打卡"+a+"次");
                //register("dates");

            }
        });
        LxBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowWenDuActivity.class);
                startActivity(intent);
                //finish();

            }
        });
        setBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SysSettingActivity.class);
                startActivity(intent);
            }
        });
    }
    private void register(String daka){
        //获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("daka",MODE_PRIVATE);
        //获取Editor对象的引用
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //将获取过来的值放入文件
        editor.putInt(daka, m+1);
        //editor.putString(loginUserPassword, loginUserPassword);
        // 提交数据
        editor.commit();

    }
    private int getdates(String daka){
        SharedPreferences sharedPreferences= getSharedPreferences("daka", MODE_PRIVATE);
        int passWord = sharedPreferences.getInt(daka,0);
        return passWord;

    }

}